package com.wnxy.sddfp.sms.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.wnxy.sddfp.sms.config.SmsTemplate;
import com.wnxy.sddfp.sms.service.SmsService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
public class SmsServiceImpl implements SmsService {

    public final SmsTemplate smsTemplate;
    public final StringRedisTemplate stringRedisTemplate;

    public SmsServiceImpl(SmsTemplate smsTemplate, StringRedisTemplate stringRedisTemplate) {
        this.smsTemplate = smsTemplate;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public void sendCode(String phone) {
        // 验证码还没有过期，直接返回
        if (stringRedisTemplate.hasKey(phone)){
            return;
        }
        String code = RandomUtil.randomNumbers(4);
        // 验证码存储到redis
        stringRedisTemplate.opsForValue().set(phone,code, Duration.ofMinutes(5));
        // 调用阿里云sms服务，发送验证码
        //smsTemplate.sendSms(phone,code);
    }
}
